package com.cskaoyan.service;

import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.IssueExample;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.utils.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public ListData<Issue> queryList(Integer page, Integer limit, String question, String sort, String order) {

        // 修改预编译的sql语句
        PageHelper.startPage(page, limit);
        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(sort + " " + order);
        IssueExample.Criteria criteria = issueExample.createCriteria();
        if (question != null) {
            criteria.andQuestionLike("%" + question + "%");
        }

        criteria.andDeletedEqualTo(false);

        // issue 数据
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        // 记录总数
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues);

        ListData<Issue> listData = new ListData<Issue>();
        listData.setItems(issues);
        listData.setTotal(issuePageInfo.getTotal());
        return listData;
    }

    @Override
    public Issue create(Issue issue) {
        issue.setAddTime(DateUtils.getCurrentTime());
        issue.setUpdateTime(DateUtils.getCurrentTime());
        issueMapper.insertSelective(issue);
        return issue;
    }

    @Override
    public int delete(Issue issue) {
        issue.setDeleted(true);
        int i = issueMapper.updateByPrimaryKeySelective(issue);
        return i;
    }

    @Override
    public int update(Issue issue) {
        issue.setDeleted(null);
        int i = issueMapper.updateByPrimaryKeySelective(issue);
        return i;
    }
}
